var QQMapWX = require('../../utils/qqmap.js');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
  key: 'KOOBZ-5UUEX-EYL42-ZCZXL-AABJE-DWBNU' // 必填
});
let plugin = requirePlugin('routePlan');
let key = 'KOOBZ-5UUEX-EYL42-ZCZXL-AABJE-DWBNU';  //使用在腾讯位置服务申请的key
let referer = '定位';   //调用插件的app的名称
// pages/home/home.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    latitude: "",
    longitude: "",
    markets: [{
        id: 1,
        latitude: "",
        longitude: "",
        iconPath: ""
      },
    ],
    address: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
  onShow() {
    this.getLocation();
  },
  getLocation() {
    console.log("00")
    wx.getSetting({
      success: (res) => {
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        // wx.getSetting获取授权Api  wx.openSetting帮助用户打开设置api
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      this.location()
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          this.location()
        } else {
          //调用wx.getLocation的API
          this.location()
        }
      }
    })
  },
  location() {
    const _this = this;
    wx.getLocation({
      success(res) {
        console.log(res);
        _this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        console.log(_this.data.latitude);
      }
    })

  },
  // 获取地址
  getAddress(e) {
    console.log(e);
    this.setData({
      address: e.detail.value
    })
    console.log(qqmapsdk);
    this.backAddress();
  },
  // 你地址解析
  backAddress() {
    const _this = this;
    qqmapsdk.geocoder({
        address: _this.data.address,
        success(res) {
          console.log(res);
          _this.setData({
            markets: [{
              id: 1,
              latitude: res.result.location.lat,
              longitude: res.result.location.lng,
              iconPath: "../../assets/icon/address.png",
              width:20,
              height:20
            }],
          });
          _this.showTargetMap();
        }
      },

    )
  },
  // 让地图里的目标地址展示在页面
  showTargetMap(){
   const map= wx.createMapContext("mapId");
   map.includePoints({
    padding:[50,50,50,50],
    points:[{
      longitude:this.data.longitude,
      latitude:this.data.latitude
    },{
      longitude:this.data.markets[0].longitude,
      latitude:this.data.markets[0].latitude
    }]
   });

  //  将自身位置展示在地图中间
 
  },
  toSelf(){
    const map= wx.createMapContext("mapId");
     map.moveToLocation();
  },
  toTargetPath(){
    const _this=this;
    console.log(_this.data.markets);
    let endPoint = JSON.stringify({  //终点
      'name': _this.data.address,
      'latitude': _this.data.markets[0].latitude,
      'longitude': _this.data.markets[0].longitude
    });
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer +
       '&endPoint=' + endPoint
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})